var _tools = require("../../utils/tools");

function _toConsumableArray(t) {
    if (Array.isArray(t)) {
        for (var e = 0, a = Array(t.length); e < t.length; e++) a[e] = t[e];
        return a;
    }
    return Array.from(t);
}

function _defineProperty(t, e, a) {
    return e in t ? Object.defineProperty(t, e, {
        value: a,
        enumerable: !0,
        configurable: !0,
        writable: !0
    }) : t[e] = a, t;
}

var g, app = getApp(), page = app.page, request = app.util.request, route = app.route, toast = app.toast;

function getType() {
  if(g.data.system.is_force_login){
    return void (0, _tools.verifyLogin)({
      success: function() { 
        request({
          url: "LawType",
          data:{
            type: g.data.category,
          },
          showLoading: !1,
          success: function(t) {
              t = [ {
                  title: "全部",
                  id: -1
              } ].concat(_toConsumableArray(t));
              for (var e = 0; e < t.length; e++) t[e].page = 1, t[e].ok = !1, g.options.id == t[e].id && 0 == g.data.curr && g.setData({
                  curr: e
              });
              g.setData({
                  type: t
              }, function() {
                  getQuestionList();
                  g.setData({
                      curr: g.data.curr
                  });
              });
          }
      });
      }
    })
  }
    
}

function getQuestionList() {
  var n = 0 < arguments.length && void 0 !== arguments[0] ? arguments[0] : "", o = g.data.curr, s = g.data.type[o].page, i = "type[" + o + "].page";
  g.data.type[o].ok || request({
      url: "QuestionList",
      data: {
          type_id: n,
          number: app.config.wenda_number,
          page: s,
          type: g.data.category
      },
      success: function(t) {
          if (n) {
              g.setData(_defineProperty({}, i, s + 1));
              var e = g.data.list[o];
              e && 1 != s && (t = [].concat(_toConsumableArray(e), _toConsumableArray(t))), i = "list[" + o + "]", 
              g.setData(_defineProperty({}, i, t)), t.length && s * app.config.wenda_number >= t[0].num && (i = "type[" + o + "].ok", 
              g.setData(_defineProperty({}, i, !0))), setIsload();
          } else {
              for (var a = g.data.type, r = 0; r < t.length; r++) t[r][0] && t[r].length == t[r][0].num && (a[r].ok = !0);
              g.setData({
                  list: t,
                  type: a
              });
          }
      }
  });
}

function setIsload() {
    var t = 0 < arguments.length && void 0 !== arguments[0] && arguments[0], e = "type[" + g.data.curr + "].isLoad";
    g.setData(_defineProperty({}, e, t));
}

page({
    data: {
        curr: 0,
        type: ''
    },
    watch:{
      userInfo:function(newValue){
        if(newValue){
          getType()
        }
      }
    },
    onLoad: function(t) {
      getApp().setWatcher(this.data, this.watch);
      this.setData({
        category: t.type
      })
        g = this, app.system && (app.system.wenda && "0" != app.system.wenda.state || wx.showModal({
            title: "提示",
            content: "未开启问答,请在后台开启问题",
            showCancel: !1,
            confirmText: "返回首页",
            success: function(t) {
                t.confirm && route({
                    type: "reLaunch",
                    url: "loading"
                });
            }
        }));
        let userInfo = wx.getStorageSync("userInfo")
        if(userInfo){
          request({
            url: "AddUsersLog",
            data: {
              user_id: userInfo.id,
              url: '/wenda/index/index',
              title: "在线咨询"
            },
            success: function(a) {
            }
          })
        }
    },
    onShow: function() {
      console.log(888)
        getType();
    },
    pullDownRefresh: function() {
        var t, e = g.data.curr, a = "list[" + e + "]", r = "type[" + e + "].page", n = "type[" + e + "].ok";
        g.setData((_defineProperty(t = {}, a, ""), _defineProperty(t, r, 1), _defineProperty(t, n, !1), 
        t), function() {
            getQuestionList(g.data.type[e].id);
        });
    },
    chooseNav: function(t) {
        t = parseInt(t.currentTarget.dataset.index), g.setData({
            curr: t
        });
    },
    swiperChange: function(t) {
        t = parseInt(t.detail.current), g.setData({
            curr: t
        });
    },
    toAdd: function() {
        (0, _tools.verifyLogin)({
            success: function(t) {
                return route({
                    type: "navigate",
                    url: "wenda_add"
                });
            }
        });
    },
    reachBottom: function() {
        getQuestionList(g.data.type[g.data.curr].id);
    }
});